package com.hex.liyue.usermodule.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.AssetsUtils;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.picker.AddressPicker;
import com.hex.liyue.usermodule.bean.AddressResultBean;
import com.hex.liyue.usermodule.presenter.EditAddressPresenter;
import com.hex.liyue.usermodule.presenter.IEditAddressView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/20.
 * 这是一个编辑地址的界面
 */
public class EditAddressActivity extends BaseActivity implements IEditAddressView, AddressPicker.OnAddressPickListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.et_mobile)
    EditText etMobile;
    @Bind(R.id.et_address_details)
    EditText etAddressDetails;
    @Bind(R.id.tv_address)
    TextView tvAddress;

    private int type;//1.编辑 2.新增
    private String delivery_id;
    private EditAddressPresenter editAddressPresenter;

    private String provinceId;
    private String provinceName;
    private String cityId;
    private String cityName;
    private String districtId;
    private String districtName;


    @Override
    protected void bindPresenter() {
        editAddressPresenter = new EditAddressPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        L.d("WJ", "initData=" + type);
        if (type == 1) {
            AddressResultBean.DataBean dataBean = getIntent().getParcelableExtra("dataBean");
            etUsername.setText(dataBean.username);
            etMobile.setText(dataBean.mobile);
            etAddressDetails.setText(dataBean.address_detail);
            provinceId = dataBean.province_id;
            provinceName = dataBean.province_name;
            cityId = dataBean.city_id;
            cityName = dataBean.city_name;
            districtId = dataBean.district_id;
            districtName = dataBean.district_name;

            tvAddress.setText(provinceName + " " + cityName + " " + " " + districtName);


            delivery_id = dataBean.delivery_id;
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_edit_address;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);

        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            tvToobarTitle.setText("编辑收获地址");
        } else if (type == 2) {
            tvToobarTitle.setText("新增收获地址");
        }
    }


    @OnClick({R.id.iv_toobar_back, R.id.tv_save_address, R.id.rl_address_box})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_save_address:
                editAddressPresenter.submitAddress();
                break;
            case R.id.rl_address_box:
                showCityDialog();
                break;
        }
    }

    private void showCityDialog() {
        ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
        String json = AssetsUtils.readText(this, "city.json");
        data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
        AddressPicker picker09 = new AddressPicker(this, data);
        if(!TextUtils.isEmpty(provinceId)){
            picker09.setSelectedItem(provinceId, cityId, districtId);
        }
        picker09.setOnAddressPickListener(this);
        picker09.show();
    }


    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public int getType() {
        return type;
    }

    @Override
    public String getDeliveryId() {
        return delivery_id;
    }

    @Override
    public String getUsername() {
        return etUsername.getText().toString();
    }

    @Override
    public String getMobile() {
        return etMobile.getText().toString();
    }

    @Override
    public String getProvinceId() {
        return provinceId;
    }

    @Override
    public String getProvinceName() {
        return provinceName;
    }

    @Override
    public String getCityId() {
        return cityId;
    }

    @Override
    public String getCityName() {
        return cityName;
    }

    @Override
    public String getDistrictId() {
        return districtId;
    }

    @Override
    public String getDistrictName() {
        return districtName;
    }


    @Override
    public String getAddressDetails() {
        return etAddressDetails.getText().toString();
    }

    @Override
    public void closeCurrentActivity() {
        setResult(RESULT_OK);
        finish();
    }


    @Override
    public void onAddressPicked(String provinceId, String provinceName, String cityId, String cityName, String districtId, String districtName) {
        this.provinceId = provinceId;
        this.provinceName = provinceName;
        this.cityId = cityId;
        this.cityName = cityName;
        this.districtId = districtId;
        this.districtName = districtName;
        L.d("WJ", "provinceId=" + provinceId + "==provinceName==" + provinceName);
        L.d("WJ", "cityId=" + cityId + "==cityName==" + cityName);
        L.d("WJ", "districtId=" + districtId + "==districtName==" + districtName);
        tvAddress.setText(provinceName + " " + cityName + " " + districtName);
    }
}
